<template>
  <div>
    <el-dialog
      title="设置密码"
      :visible.sync="passDialogVisible"
      :close-on-click-modal='false'
      :close-on-press-escape='false'
      width="30%"
      :before-close="close">
      <el-form ref="emailForm"
               :model="passForm"
               :rules="passRule">
        <el-form-item 
                      v-if="passStr"
                      label="原密码">
          <el-input show-password
                    v-model="passForm.oldPass"></el-input>
        </el-form-item>
          <el-form-item 
                      required
                      prop="newPass"
                      label="新密码">
            <el-input v-model="passForm.newPass" show-password placeholder=""></el-input>
          </el-form-item>
          <el-form-item 
                      required
                      prop="newPassT"
                      label="确认新密码">
            <el-input v-model="passForm.newPassT" show-password placeholder=""></el-input>
          </el-form-item>
      </el-form>
      <div slot="footer">
        <el-tooltip effect="dark" content="确定" placement="top">
          <el-button type="primary" icon="el-icon-check" @click="confrim"></el-button>
        </el-tooltip>
        <el-tooltip effect="dark"  content="取消" placement="top">
           <el-button @click="close" icon="el-icon-close" ></el-button>
        </el-tooltip>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {postPassword,updatePassword } from '../index.js'
  export default {
    data() {
      return {
        passForm:{},
        passRule: {
          newPass:[{ required: true, message: '请输入', trigger: 'change' }],
          newPassT: [{ required: true, message: '请输入', trigger: 'change' }],
        },
      }
    },
    mounted(){
      console.log(21);
    },
    methods: {
      close(){
        this.$emit('close')
      },
      dialogVisible(){

      },
      confrim(){ 
        const that =this;
         this.$refs.emailForm.validate((valid) => {
          if (valid) {
            if(that.passForm.newPass!=that.passForm.newPassT){
              that.passForm.newPass='';
              that.passForm.newPassT='';
              that.$message.warning('密码输入不一致请重新输入');
            }
            let formData = new FormData();
            if(this.passStr){
              formData.append('oldPass',that.passForm.oldPass);
              formData.append('newPass',that.passForm.newPass);
              updatePassword(formData).then(res=>{
                 if(res.data.meta.success){
                  that.$message.success(res.data.meta.message);
                  that.close();
                }
              })
            }else{
              formData.append('password',that.passForm.newPass)
              postPassword(formData).then(res=>{
                if(res.data.meta.success){
                  that.$message.success(res.data.meta.message);
                  that.close();
                }
              })
            }
          }
        }); 
      },
    },
    props:{
      passDialogVisible:Boolean,
      passStr:[String,Number],
    }
  }
</script>

<style lang="scss" scoped>

</style>